home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / svaecopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.4 KB  |  144 lines

  1. /*
  2.     File:        SVAECopy.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25. #include "SVAECopy.h"
  26.  
  27. #include "SVEditAEUtils.h"
  28. #include "SVEditWindow.h"        // for DPtrFromWindowPtr()
  29.  
  30. #include "SVAESelect.h"
  31.  
  32. #include <Scrap.h>
  33.  
  34.  
  35. #pragma segment AppleEvent
  36.  
  37. // Handle a copy to scrap e.g 'copy last word of document 1'
  38. // Note that 'copy last word of document 1 to end of document 2' is a kAEClone event
  39.      
  40. pascal OSErr    DoCopy(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  41. {
  42. #pragma unused (reply, refcon)
  43.  
  44.     AEDesc        directObj = {typeNull, NULL};
  45.     TextToken    aTextToken;
  46.     short        ignore;
  47.     OSErr        err;
  48.  
  49.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  50.     // If we get an error here it just means that they haven't supplied a reference to
  51.     // an object to copy - so copy the current section instead.
  52.     
  53.     if (directObj.descriptorType != typeNull)
  54.         err = CopyDesc(&directObj);
  55.     else
  56.     {            // Just copy the selection of the front window
  57.         err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
  58.         if (noErr != err) goto done;
  59.         
  60.         err = CopyTextToken(&aTextToken);
  61.     }
  62.  
  63. done:    
  64.     if (directObj.dataHandle)
  65.         AEDisposeDesc(&directObj);
  66.         
  67.     return(err);
  68. } // DoCopy
  69.  
  70.  
  71. OSErr    CopyTextToken(TextToken* theToken)
  72. {
  73.     WindowPtr        aWindow;
  74.     DPtr            docPtr;
  75.     OSErr            err;
  76.     
  77.     aWindow = theToken->tokenWindow;
  78.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  79.     
  80.     if (! aWindow || ! docPtr)
  81.         return(errAENoSuchObject);
  82.  
  83.                     // Set this tokens selection
  84.     err = SelectTextToken(theToken);
  85.     if (noErr != err) goto done;
  86.  
  87.     err = (OSErr)ZeroScrap();
  88.     TECopy(docPtr->theText);     
  89.     
  90. done:
  91.     return(err);
  92. }
  93.  
  94. OSErr    CopyTextDesc(AEDesc* textDesc)
  95. {
  96.     TextToken        aTextToken;
  97.     Size            actualSize;
  98.     OSErr            err;
  99.  
  100.     if (typeMyText != textDesc->descriptorType)
  101.         return(errAETypeError);
  102.         
  103.     GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
  104.  
  105.     err = CopyTextToken(&aTextToken);
  106.     
  107.     return(err);
  108. }
  109.  
  110. OSErr    CopyDesc(AEDesc* aDesc)
  111. {
  112.     AEDesc        copyDesc = {typeNull, NULL},
  113.                 textDesc = {typeNull, NULL};
  114.     OSErr        err;
  115.     
  116.     if (typeObjectSpecifier == aDesc->descriptorType)
  117.         err = AEResolve(aDesc, kAEIDoMinimum, ©Desc);
  118.     else
  119.         err = AEDuplicateDesc(aDesc, ©Desc);
  120.         
  121.     if (noErr != err) goto done;
  122.     
  123.     switch (copyDesc.descriptorType)
  124.     {
  125.         case typeAEList:
  126.             err = errAETypeError;
  127.             // We can't handle copying more than one item to the scrap
  128.             break;
  129.             
  130.         default:
  131.             err = AECoerceDesc(©Desc, typeMyText, &textDesc);
  132.             if (noErr != err) goto done;
  133.             err = CopyTextDesc(&textDesc);
  134.     }
  135.     
  136. done:
  137.     if (copyDesc.dataHandle)
  138.         AEDisposeDesc(©Desc);
  139.     if (textDesc.dataHandle)
  140.         AEDisposeDesc(&textDesc);
  141.     
  142.     return(err);
  143. }
  144.